package com.jzo2o.customer.controller.consumer;


import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 区域表 前端控制器
 * </p>
 *
 * @author itcast
 * @since 2023-07-03
 */
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 用户地址相关接口")
public class AddressBookController {

    @Autowired
    private IAddressBookService addressBookService;

    /**
     * 查询用户默认地址
     */
    @GetMapping("/defaultAddress")
    @ApiOperation("查询用户默认地址")
    public AddressBookResDTO findDefaultAddress() {
        return addressBookService.findDefaultAddress();
    }

    /**
     * 新增地址
     * @param dto
     */
    @PostMapping()
    @ApiOperation("新增地址")
    public void addNewAddressBook(@RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.addNewAddressBook(dto);
    }

    /**
     * 获取地址列表
     * @return 地址列表
     */
    @ApiOperation("地址薄分页查询")
    @GetMapping("/page")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO) {
        return addressBookService.page(addressBookPageQueryReqDTO);
    }

    /**
     * 根据地址id查询地址
     * @param id
     * @return 地址
     */
    @GetMapping("/{id}")
    @ApiOperation("根据地址id查询地址")
    public AddressBookResDTO findAddressBookByUserId(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return BeanUtil.toBean(addressBook, AddressBookResDTO.class);
    }

    /**
     * 修改地址
     * @param id

     */
    @ApiOperation("地址薄修改")
    @PutMapping("/{id}")
    public void update(@PathVariable("id") Long id, @RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.updateAddressBook(id, addressBookUpsertReqDTO);
    }

    /**
     * 批量删除地址
     * @param ids
     */
    @ApiOperation("地址薄批量删除")
    @DeleteMapping("/batch")
    public void logicallyDelete(@RequestBody List<Long> ids) {
        addressBookService.removeByIds(ids);
    }

    /**
     * 设置地址为默认/取消默认
     * @param id
     * @param flag
     */
    @PutMapping("/default")
    @ApiOperation("地址薄设为默认/取消默认")
    public void updateDefaultStatus(Long id, Integer flag) {
        addressBookService.updateDefaultStatus(id, flag);
    }
}
